package com.design.masterWorker;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

public class Master {
    //�������
    protected Queue<Object> workQuery = new ConcurrentLinkedQueue<Object>();
    //worker���̶���
    protected Map<String, Thread> threadMap = new HashMap<>();
    //������������
    protected Map<String, Object> resultMap = new ConcurrentHashMap<>();

    //Master �Ĺ��죬��Ҫһ��Worker �����߼�������Ҫ��Worker��������
    public Master(Worker worker, int countWorker){
        worker.setWorkQueue(workQuery);
        worker.setResultMap(resultMap);
        for (int i = 0; i < countWorker; i++) {
            threadMap.put(Integer.toString(i),new Thread(worker));
        }
    }
    //�Ƿ����е������񶼽�����
    public boolean isComplete() {
        for (Map.Entry<String, Thread> entry : threadMap.entrySet()) {
            if (entry.getValue().getState()!=Thread.State.TERMINATED){
                return false;
            }
        }
        return true;
    }
    //�ύһ������
    public void submit(Object job){
        workQuery.add(job);
    }
    //��������������
    public Map<String,Object> getResultMap(){
        return resultMap;
    }

    //��ʼ�������е�worker���̣����д���
    public void  execute(){
        for (Map.Entry<String,Thread> entry : threadMap.entrySet()){
            entry.getValue().start();
        }
    }

}
